package com.jdd.modules.parking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.constant.CacheConstant;
import com.jdd.modules.parking.entity.ConfigParkThrough;
import com.jdd.modules.parking.mapper.ConfigParkThroughMapper;
import com.jdd.modules.parking.service.IConfigParkThroughService;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 车场通行配置
 * @Author: jdd
 * @Date: 2021-02-18
 * @Version: V1.0
 */
@Service
public class ConfigParkThroughServiceImpl extends ServiceImpl<ConfigParkThroughMapper, ConfigParkThrough> implements IConfigParkThroughService {

    @Resource
    private ConfigParkThroughMapper configParkThroughMapper;

    /**
     * 根据设备IP和车场编码获取此设备的通行配置
     *
     * @param serialNo 设备序列号
     * @return 通行配置
     */
    @Override
    @Cacheable(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, key = "#serialNo", unless = "#result==null")
    public ConfigParkThrough queryConfigParkThrough(@Param("serialNo") String serialNo) {
        LambdaQueryWrapper<ConfigParkThrough> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigParkThrough::getSerialNo, serialNo);
        return this.getOne(queryWrapper);
    }

    /**
     * 获取车场下所有的通行配置
     *
     * @return 通行配置列表
     */
    @Override
    public List<ConfigParkThrough> findConfigParkThroughByParkCode() {
        LambdaQueryWrapper<ConfigParkThrough> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }

    /**
     * 根据id获取通行配置
     *
     * @param id 通行配置id
     * @return 通行配置
     */
    @Override
    @Cacheable(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, key = "#id", unless = "#result==null")
    public ConfigParkThrough findById(String id) {
        return this.getById(id);
    }

    /**
     * 删除对应车场编号的车场下的所有的计费规则信息
     *
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, allEntries = true),
    })
    public Boolean removeByParkCode() {
        LambdaQueryWrapper<ConfigParkThrough> queryWrapper = new LambdaQueryWrapper<>();
        return this.remove(queryWrapper);
    }

    /**
     * 保存通行配置实体
     *
     * @param configParkThrough 通行配置
     * @return 保存结果
     */
    @Override
    @Caching(evict = {@CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, allEntries = true)})
    public boolean saveEntity(ConfigParkThrough configParkThrough) {
        if (configParkThroughMapper.insert(configParkThrough) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 更新通行配置实体
     *
     * @param configParkThrough 通行配置
     * @return 更新结果
     */
    @Override
    @Caching(evict = {@CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, allEntries = true)})
    public boolean updateEntity(ConfigParkThrough configParkThrough) {
        if (configParkThroughMapper.updateById(configParkThrough) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 根据id删除对应的通行配置实体
     *
     * @param id 通行配置id
     * @return 删除状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, allEntries = true)})
    public boolean deleteEntityById(String id) {
        if (configParkThroughMapper.deleteById(id) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 根据id列表删除通行配置
     *
     * @param ids id列表
     * @return 删除状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, allEntries = true)})
    public boolean deleteEntityByIds(List<String> ids) {
        if (configParkThroughMapper.deleteBatchIds(ids) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 通过设备序列号删除通行配置
     *
     * @param serialNo 设备序列号
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, allEntries = true)})
    public boolean deleteEntityBySerialNo(String serialNo) {
        LambdaQueryWrapper<ConfigParkThrough> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigParkThrough::getSerialNo, serialNo);
        if (configParkThroughMapper.delete(queryWrapper) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 清空表数据
     *
     * @return true 清空成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_SERIAL_NO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_THROUGH_BY_ID, allEntries = true)})
    public boolean removeAllInfo() {
        try {
            configParkThroughMapper.removeAllInfo();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
